package com.itheima.bos.web.action.take_delivery;

import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;

import org.apache.commons.io.FileUtils;
import org.apache.struts2.ServletActionContext;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.ParentPackage;
import org.apache.struts2.convention.annotation.Result;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import com.itheima.bos.domain.take_delivery.Promotion;
import com.itheima.bos.web.action.base.BaseAction;
import com.opensymphony.xwork2.ActionContext;

/**+
 * 在线编辑器的action
 * @author yanju
 *
 */
@Namespace("/")
@ParentPackage("json-default")
@Scope("prototype")
@Controller
public class ImgicAction extends BaseAction<Object>{
	
	//接收上传文件参数
	private File imgFile;
	private String imgFileFileName;
	private String imgFileFileContent;
	public void setImgFile(File imgFile) {
		this.imgFile = imgFile;
	}
	public void setImgFileFileName(String imgFileFileName) {
		this.imgFileFileName = imgFileFileName;
	}
	public void setImgFileFileContent(String imgFileFileContent) {
		this.imgFileFileContent = imgFileFileContent;
	}
	
	
	@Action(value="promotion_uploadImgFile",results={@Result(name="success",type="json")})
	public String promotion_uploadImgFile() throws Exception, IOException{
		//设置保存图片的路径
		String savePath=ServletActionContext.getServletContext().getRealPath("/upload/");
		//设置保存的地址
		String saveUrl=ServletActionContext.getRequest().getContextPath()+"/upload/";
		//将传进来的图片以uuid的名字改名保存
		  UUID randomUUID = UUID.randomUUID();
		  //得到文件格式
		  String form = imgFileFileName.substring(imgFileFileName.lastIndexOf("."));
		  //得到文件名
		  String newFileName=randomUUID+form;
		//保存图片(绝对路径)
		  FileUtils.copyFile(imgFile, new File(savePath+"/"+newFileName));
		  //通知浏览器上传成功
		  Map<String,Object> map = new HashMap<String,Object>();
		  map.put("error", 0);
		  map.put("url",saveUrl+newFileName);//返回的是相对路径
		  //将结果返回
		 ActionContext.getContext().getValueStack().push(map);
		return SUCCESS;
	}
	
	/*
	 * 在图片管理器中选取文件
	 */
	@Action(value="promotion_ImgManage",results={@Result(name="success",type="json")})
	public String promotion_ImgManage(){
		//得到已保存文件的路径
		String rootPath=ServletActionContext.getServletContext().getRealPath("/")+"upload/";
		//得到访问图片的的路径
		String rootUrl=ServletActionContext.getRequest().getContextPath()+"/upload/";
		
		// 遍历目录取的文件信息
		List<Map<String, Object>> fileList = new ArrayList<Map<String, Object>>();
		
		//得到图片保存路径对应的文件夹
		File currentPathFile = new File(rootPath);
		// 图片扩展名
		String[] fileTypes = new String[] { "gif", "jpg", "jpeg", "png", "bmp" };
		if (currentPathFile.listFiles() != null) {
			for (File file : currentPathFile.listFiles()) {
				Map<String, Object> hash = new HashMap<String, Object>();
				String fileName = file.getName();
				if (file.isDirectory()) {
					hash.put("is_dir", true);
					hash.put("has_file", (file.listFiles() != null));
					hash.put("filesize", 0L);
					hash.put("is_photo", false);
					hash.put("filetype", "");
				} else if (file.isFile()) {
					String fileExt = fileName.substring(
							fileName.lastIndexOf(".") + 1).toLowerCase();
					hash.put("is_dir", false);
					hash.put("has_file", false);
					hash.put("filesize", file.length());
					hash.put("is_photo", Arrays.<String> asList(fileTypes)
							.contains(fileExt));
					hash.put("filetype", fileExt);
				}
				hash.put("filename", fileName);
				hash.put("datetime",new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(file.lastModified()));
				fileList.add(hash);
			}
		}
		Map<String, Object> result = new HashMap<String, Object>();
		result.put("moveup_dir_path", "");
		result.put("current_dir_path", rootPath);
		result.put("current_url", rootUrl);
		result.put("total_count", fileList.size());
		result.put("file_list", fileList);
		return SUCCESS;
	}
	
	
	
}
